package com.chase.flashcleaner.service

import com.chase.flashcleaner.entity.CleanPath
import com.chase.flashcleaner.utils.logd
import okhttp3.OkHttpClient
import okhttp3.Request

object NetworkService {
    fun getCleanRawPaths(): String? {
        val url = "https://gushichen.gitee.io/resources/apps/flash-cleaner/clean-paths.json"
        val okHttpClient = OkHttpClient()
        val request = Request.Builder()
            .url(url)
            .removeHeader("User-Agent")
            .addHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4)")
            .build()
        val call = okHttpClient.newCall(request)

        val response = call.execute()
        val body =  response.body()?.string()
        logd("$body", "getCleanPaths")
        return body
    }

    fun getCleanPaths(): ArrayList<CleanPath> {
        val rawPaths = getCleanRawPaths()
        val paths = rawPaths?.split('\n')
        val cleanedPaths = ArrayList<CleanPath>()
        paths?.forEach {
            val temp = it.split(' ')
            cleanedPaths.add(CleanPath(temp[0], temp[1], temp[2].toBoolean()))
        }
        return cleanedPaths
    }
}